var userCount = 0;
var pageSize = 10;
function userList(page, UserNickName, StartTime, EndTime) {
    $.ajax({
        url: "/PersonalCenter/GetUserList",
        type: "post",
        data: { page: page, rows: pageSize, UserNickName: UserNickName, StartTime: StartTime, EndTime: EndTime },
        dataType: "json",
        async: false,
        success: function (list) {
            userCount = list.total;
            $('#dg_UserList').html("<tr><th>用户帐号</th><th>用户类型</th><th>当前总投注</th><th>注册时间</th></tr>")
            if (list.total > 0) {
                $.each(list.rows, function (i, n) {
                    $('#dg_UserList').append("<tr><td>" + n["username"] + "</td>" +
                    "<td>" + uType(n["status"]) + "</td>" +
                    "<td>" + n["betcountmoney"] + "</td>" +
                    "<td>" + Datetime(n["regtime"]) + "</td></tr>");
                });
            }
            else {
                $('#dg_UserList').append("<tr><td colspan=\"4\">暂时没有数据...</td></tr>");
            }
        }
    });
}

userList(1, "", "", "");
//分页
$("#userPage").createPage({
    pageCount: userCount,
    pageSize: pageSize,
    current: 1,
    backFn: function (p) {
        //userList(p, $("#txt_UserNickName").val(), $('#StartTime').datebox('getValue'), $('#EndTime').datebox('getValue'));
        userList(p, "", "", "");
    }
});

$("#userListAdd").click(function () {
    $("#UserList_Dialog").dialog({
        title: "用户添加",
        href: "/PersonalCenter/UserDtail",
        width: "auto",
        height: "auto",
        iconCls: "icon-add",
        modal: true,
        closed: false,
        buttons: [{
            text: '保存',
            handler: function () {
                if ($("#UserName").val() == "") {
                    $.messager.alert('提示:', "用户帐号不能为空", "error", function () {
                        $("#UserName").focus();
                    });
                    return;
                }
                if ($("#UserPwd").val() == "") {
                    $.messager.alert('提示:', "用户密码不能为空", "error", function () {
                        $("#UserPwd").focus();
                    });
                    return;
                }
                $.ajax({
                    url: "/PersonalCenter/AddOrEditUser",
                    type: "post",
                    data: {
                        id: $("#id").val(), UserName: $("#UserName").val(), UserNickName: $("#UserNickName").val()
                        , UserPwd: $("#UserPwd").val(), UserType: 0, Phone: $("#Phone").val(), QQ: $("#QQ").val(), Email: $("#Email").val(), Address: $("#Address").val()
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.Success) {
                            userList(1, "", 0, 0, "", "");
                            $("#UserList_Dialog").dialog("close");

                        } else {
                            $.messager.alert('提示:', data.Msg, "error");
                        }
                    }
                });
            }
        }, {
            text: '取消',
            handler: function () {
                userList(1, "", 0, 0, "", "");
                $("#UserList_Dialog").dialog("close");
            }
        }]
    });
});

$("#btnULSearch").click(function () {
    userList(1, $("#txt_UserNickName").val(), $('#StartTime').datebox('getValue'), $('#EndTime').datebox('getValue'));
});

function uType(type) {
    var res = "";
    switch (type) {
        case '1': res = "玩家"; break;
        case '0': res = "试玩"; break;
    }
    return res;
}